package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.entity.business.TpHolidaySet;
import com.ys.mapper.business.TpHolidaySetMapper;
import com.ys.service.business.TpHolidaySetService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("tpHolidaySetService")
public class TpHolidaySetServiceImpl implements TpHolidaySetService {


	@Autowired
	private TpHolidaySetMapper tpHolidaySetMapper;

	
	@Override
	public long add(Map<String, Object> params) {
		TpHolidaySet entity = JSON.parseObject(JSON.toJSONString(params), TpHolidaySet.class);
		tpHolidaySetMapper.insert(entity);//先执行插入语句
		long id = entity.getId();//通过orderx.getId()获取你新插入数据的id值
		return id;
	}

	@Override
	public int edit(Map<String, Object> params) {
		TpHolidaySet entity = JSON.parseObject(JSON.toJSONString(params), TpHolidaySet.class);
		return tpHolidaySetMapper.update(entity);
	}


	/**
	 * 获取工作日时间
	 * @param start 开始时间
	 * @param end 结束时间
	 * @param beginNum =0时开始时间的当天算入时效；=1时开始时间的当天没有算入时效
	 * @return
	 */
	@Override
	public int checkOverTime(Date start, Date end, int beginNum){

		//获取假期设置数据
		List<TpHolidaySet> tpHolidaySetList = new TpHolidaySet()
				.queryList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int overTime = 0;//累计时间
		try {
			start = sdf.parse(sdf.format(start));
			end = sdf.parse(sdf.format(end));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long startTime = start.getTime();
		long endTime = end.getTime();
		long betweenDays = (endTime - startTime) / (1000 * 3600 * 24);
		//开始时间和结束时间相差的天数
		int dsyNum = Integer.parseInt(String.valueOf(betweenDays));
		if(dsyNum>0){//大于一天的时候才计算超时时间

			//计算超时时间
			Calendar calendar = null;
			//j=1,制单当天不算时效
			for(int j=beginNum;j<=dsyNum;j++){
				calendar=Calendar.getInstance();
				calendar.setTime(start);
				calendar.add(Calendar.DAY_OF_MONTH, +j);
				Date thatDate = calendar.getTime();
				String thatDay = sdf.format(thatDate);
				//判断该日是否是休息日
				boolean hasSetHoliDay = false;//标识该日是否设置
				if(tpHolidaySetList!=null && tpHolidaySetList.size()>0){
					for (int h=0;h<tpHolidaySetList.size();h++){
						String holiday = sdf.format(tpHolidaySetList.get(h).getSetDate());
						if(thatDay.equals(holiday)){
							hasSetHoliDay = true;
							if(tpHolidaySetList.get(h).getIsHoliday()==0){//假期
								//overTime --;
							}else {//工作日
								overTime ++;
							}
							break;
						}
					}
				}
				//若没有被设置，则再判断该天是周末还是工作日
				if(!hasSetHoliDay){
					String[] weeks = {"7","1","2","3","4","5","6"};
					Calendar cal1 = Calendar.getInstance();
					cal1.setTime(thatDate);
					int week_index = cal1.get(Calendar.DAY_OF_WEEK) - 1;
					if(week_index<0){
						week_index = 0;
					}
					String weekNum = weeks[week_index];
					if(weekNum.equals("7") || weekNum.equals("6")){
						//overTime --;
					}else {//工作日
						overTime ++;
					}
				}
			}
		}
		return overTime;
	}


	/**
	 * 根据开始时间，工作时效时间，根据假期设置计算结束时间
	 * @param start 开始时间
	 * @param workDay 工作时效时间
	 * @param beginNum =0时开始时间的当天算入时效；=1时开始时间的当天没有算入时效
	 * @return
	 */
	@Override
	public Date countEndTimeSkipWorkDate(Date start,int workDay, int beginNum){
		//获取假期设置数据
		List<TpHolidaySet> tpHolidaySetList = new TpHolidaySet()
				.queryList();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		int workDayCount = 0;//存放计算过的时效
		int allDay = 0;//存放总共的时间
		Calendar calendar = null;
		for(int i=beginNum;;i++){
			calendar=Calendar.getInstance();
			calendar.setTime(start);
			calendar.add(Calendar.DAY_OF_MONTH, +i);
			Date thatDate = calendar.getTime();
			String thatDay = sdf.format(thatDate);

			//判断该日是否是休息日
			boolean hasSetHoliDay = false;//标识该日是否设置
			if(tpHolidaySetList!=null && tpHolidaySetList.size()>0){
				for (int h=0;h<tpHolidaySetList.size();h++){
					String holiday = sdf.format(tpHolidaySetList.get(h).getSetDate());
					if(thatDay.equals(holiday)){
						hasSetHoliDay = true;
						if(tpHolidaySetList.get(h).getIsHoliday()==1){//工作日
							workDayCount++;
						}//假期不做操作
						break;
					}
				}
			}

			//若没有被设置，则再判断该天是周末还是工作日
			if(!hasSetHoliDay){
				String[] weeks = {"7","1","2","3","4","5","6"};
				Calendar cal1 = Calendar.getInstance();
				cal1.setTime(thatDate);
				int week_index = cal1.get(Calendar.DAY_OF_WEEK) - 1;
				if(week_index<0){
					week_index = 0;
				}
				String weekNum = weeks[week_index];
				if(weekNum.equals("7") || weekNum.equals("6")){

				}else {//工作日
					workDayCount ++;
				}
			}

			allDay = i;
			if(workDayCount>=workDay)break;//跳出循环
		}

		calendar=Calendar.getInstance();
		calendar.setTime(start);
		calendar.add(Calendar.DAY_OF_MONTH, +allDay);//开始时间 + （工作天数、假期天数）
		Date endDateTime = calendar.getTime();

		return endDateTime;
	}

}
